package com.shijie.controller.app;

import com.shijie.component.AppUserService;
import com.shijie.service.UserScoresService;
import com.shijie.domain.entity.UserScores;
import com.shijie.core.Result;
import com.shijie.core.ResultGenerator;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.catalina.User;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

/*
 *  积分信息等相关接口
 *
 *  @author: smart boy
 *  @date: 2018-08-06
 */
@RestController
@RequestMapping("/app/scores")
public class AppUserScoresController {

    @Resource
	private UserScoresService userScoresService;

    @Resource
    private AppUserService appUserService;

	@GetMapping
    public Result list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "10") Integer pageSize,
                       @RequestParam(required = false) Integer direct, @RequestParam(required = false) Integer type) {
        PageHelper.startPage(page, pageSize);
        UserScores qCondition = new UserScores();
        qCondition.setUserId(appUserService.getCurrentUser().getId());
        qCondition.setDirect(direct);
        qCondition.setType(type);
        List<UserScores> list = userScoresService.getScoresForUser(qCondition);
        PageInfo<UserScores> pageInfo = new PageInfo<>(list);
        return ResultGenerator.success(pageInfo);
    }

	@PostMapping
    public Result add(@RequestBody UserScores userScores) {
        userScoresService.save(userScores);
        return ResultGenerator.success();
	}

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        userScoresService.deleteById(id);
        return ResultGenerator.success();
	}

    @PutMapping
    public Result update(@RequestBody UserScores userScores) {
        userScoresService.updateById(userScores);
        return ResultGenerator.success();
	}

    @GetMapping("/{id}")
    public Result detail(@PathVariable Integer id) {
        UserScores userScores = userScoresService.selectById(id);
        return ResultGenerator.success(userScores);
	}
}

